package com.gurun.codes.business.jpush;

import android.content.Context;

import com.gurun.base.Logger;
import com.gurun.base.utils.CommonUtils;
import com.gurun.codes.App;
import com.gurun.codes.R;
import com.gurun.codes.contract.base.BasePresenter;
import com.gurun.codes.contract.base.view.RxView;
import com.gurun.codes.utils.Constants;
import com.gurun.model.base.ResultObserver;
import com.gurun.model.base.RetrofitManager;
import com.gurun.model.bean.ApiResult;
import com.gurun.model.bean.BaseRequestBean;
import com.gurun.model.bean.home.GetHomeDeviceListRequestBean;
import com.gurun.model.bean.home.GetHomeDeviceListResultBean;
import com.gurun.model.bean.home.GetHomeListResultBean;
import com.gurun.model.bean.jpush.BindPushChannelIdRequestBean;
import com.gurun.model.bean.jpush.BindPushChannelIdResultBean;
import com.gurun.model.service.HomeService;
import com.gurun.model.service.JpushService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface JpushContract {

    interface JpushView extends RxView {
    }

    class JpushPresenter extends BasePresenter<JpushView> {

        private Context mContext;

        public JpushPresenter(Context context) {
            this.mContext = context;
        }

        public void bindPushChannelIdToServer() {
            BindPushChannelIdRequestBean requestBean = new BindPushChannelIdRequestBean();
            requestBean.setVersion(Constants.HTTP_VERSION);
            requestBean.setToken("token");
            requestBean.setHardwareId(CommonUtils.getAndoridId(mContext));
            requestBean.setPushChannelId(JPushInterface.getRegistrationID(mContext));

            RetrofitManager.getInstance().createApiService(JpushService.class)
                    .bindPushChannelId(requestBean)
                    .compose(getView().<BindPushChannelIdResultBean>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new ResultObserver<BindPushChannelIdResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                        }

                        @Override
                        public void onLoaded() {
                        }

                        @Override
                        public void onSuccess(BindPushChannelIdResultBean result, String msg) {
                            Logger.d("绑定push channel id到服务器成功");
                        }

                        @Override
                        public void onApiError(ApiResult result) {
                        }
                    }));
        }

        public void bindUserPushChannelIdToServer() {
            BindPushChannelIdRequestBean requestBean = new BindPushChannelIdRequestBean();
            requestBean.setVersion(Constants.HTTP_VERSION);
            requestBean.setUserId(App.getInstance().getLoginUserId());
            requestBean.setToken(App.getInstance().getLoginToken());
            requestBean.setPushChannelId(JPushInterface.getRegistrationID(mContext));

            RetrofitManager.getInstance().createApiService(JpushService.class)
                    .bindUserPushChannelId(requestBean)
                    .compose(getView().<BindPushChannelIdResultBean>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new ResultObserver<BindPushChannelIdResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                        }

                        @Override
                        public void onLoaded() {
                        }

                        @Override
                        public void onSuccess(BindPushChannelIdResultBean result, String msg) {
                            Logger.d("绑定push channel id到服务器成功");
                        }

                        @Override
                        public void onApiError(ApiResult result) {
                        }
                    }));
        }

    }
}
